//
//  HWIntegralSubsidiaryViewController.m
//  HWanGame
//
//  Created by 陈道斌 on 2019/10/10.
//  Copyright © 2019 Chendaobin. All rights reserved.
//

#import "HWIntegralSubsidiaryViewController.h"
#import "HWIncomeMingXiViewController.h" //  收入
#import "HWSpendingMingXiViewController.h" // 支出

@interface HWIntegralSubsidiaryViewController ()<UIScrollViewDelegate>
@property (nonatomic, weak) UIScrollView *titleScrollView;
@property (nonatomic, weak) UIScrollView *contentScrollView;
@property (strong,nonatomic) UIImageView  *xiantiaoimageview;
@property (nonatomic,strong) UIButton *selectButton;
@property (nonatomic, strong) NSMutableArray *titleButtons;
@property (nonatomic,strong)  NSMutableArray *biaotiArray;

@end

@implementation HWIntegralSubsidiaryViewController

- (NSMutableArray *)titleButtons
{
    if (_titleButtons == nil) {
        _titleButtons = [NSMutableArray array];
    }
    return _titleButtons;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setupUI];
    
    
    // 1.添加标题滚动视图
    [self setupTitleScrollView];
    // 2.添加内容滚动视图
    [self setupContentScrollView];
      
    [self  setupAllChildViewController];
    [self setupAllTitle];
      

}

- (void)setupUI {
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"积分明细";
        
}


#pragma mark - 添加标题滚动视图
- (void)setupTitleScrollView
{
    // 创建titleScrollView
    UIScrollView *titleScrollView = [[UIScrollView alloc] init];
    CGFloat y = CF_NavHeight;
    
    titleScrollView.frame = CGRectMake(0, y, self.view.bounds.size.width, 40);
    // titleScrollView.backgroundColor = [UIColor redColor];
   // titleScrollView.backgroundColor = RGB(217, 247, 250);
    [self.view addSubview:titleScrollView];
    _titleScrollView = titleScrollView;
    
    
    
    
}

#pragma mark - 添加内容滚动视图
- (void)setupContentScrollView
{
    // 创建contentScrollView
    UIScrollView *contentScrollView = [[UIScrollView alloc] init];
    CGFloat y = CGRectGetMaxY(self.titleScrollView.frame);
    contentScrollView.frame = CGRectMake(0, y, self.view.bounds.size.width, self.view.bounds.size.height - y);
    [self.view addSubview:contentScrollView];
    _contentScrollView = contentScrollView;
    
    // 设置contentScrollView的属性
    // 分页
    self.contentScrollView.pagingEnabled = YES;
    // 弹簧
    self.contentScrollView.bounces = NO;
    // 指示器
    self.contentScrollView.showsHorizontalScrollIndicator = NO;
    
    contentScrollView.backgroundColor = [UIColor whiteColor];
    
    // 设置代理.目的:监听内容滚动视图 什么时候滚动完成
    self.contentScrollView.delegate = self;
    
    
    
    
}


#pragma mark - 添加所有子控制器
- (void)setupAllChildViewController
{
    self.biaotiArray = [[NSMutableArray alloc]init];
    [self.biaotiArray addObject:@"收入"];
    [self.biaotiArray addObject:@"支出"];
    
    for (int i = 0; i < self.biaotiArray.count; i++) {
        if (i ==  0) {
            HWIncomeMingXiViewController * jingrikaifu = [[HWIncomeMingXiViewController alloc]init];
            [self addChildViewController:jingrikaifu];
        } else {
            
            
            HWSpendingMingXiViewController *mingrikaifu = [[HWSpendingMingXiViewController alloc]init];
            [self addChildViewController:mingrikaifu];
            
            
        }
    }
    
    
}


#pragma mark - 设置所有标题
- (void)setupAllTitle
{
    // 已经把内容展示上去 -> 展示的效果是否是我们想要的(调整细节)
    // 1.标题颜色 为黑色
    // 2.需要让titleScrollView可以滚动
    
    // 添加所有标题按钮
    NSInteger count = self.biaotiArray.count;
    CGFloat btnW = 100;
    CGFloat btnH = self.titleScrollView.bounds.size.height;
    CGFloat btnX = 0;
    for (NSInteger i = 0; i < count; i++) {
        
        UIButton *titleButton = [UIButton buttonWithType:UIButtonTypeCustom];
        titleButton.tag = i;
        //  UIViewController *vc = self.childViewControllers[i];
        [titleButton setTitle:self.biaotiArray[i] forState:UIControlStateNormal];
        CGFloat  jiaju =  (kScreenW - count *100) / (count+ 1);
        
        btnX = i * btnW + jiaju*(i+1);
        
        titleButton.frame = CGRectMake(btnX, 0, btnW, btnH);
        [titleButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        //    titleButton.backgroundColor = [UIColor redColor];
        titleButton.titleLabel.font = [UIFont systemFontOfSize:13];
        // 监听按钮点击
        [titleButton addTarget:self action:@selector(titleClick:) forControlEvents:UIControlEventTouchUpInside];
        // 把标题按钮保存到对应的数组
        [self.titleButtons addObject:titleButton];
        
    
        
        if (i == 0) {
            [self titleClick:titleButton];
            
            UIImageView *xiantiaoimageview = [[UIImageView alloc]initWithFrame:CGRectMake(btnX+30, btnH-2, 40, 2)];
            
            xiantiaoimageview.image = [UIImage imageNamed:@""];
            xiantiaoimageview.backgroundColor = RGB(26, 113, 255);
            xiantiaoimageview.layer.cornerRadius = 1;
            xiantiaoimageview.layer.masksToBounds = YES;
            self.xiantiaoimageview = xiantiaoimageview;
            
            [self.titleScrollView addSubview:xiantiaoimageview];
            
        }
        
        [self.titleScrollView addSubview:titleButton];
    }
    
    
    
    
    
    // 设置标题的滚动范围
    self.titleScrollView.contentSize = CGSizeMake(kScreenW, 0);
    self.titleScrollView.showsHorizontalScrollIndicator = NO;
    
    // 设置内容的滚动范围
    self.contentScrollView.contentSize = CGSizeMake(count * kScreenW, 0);
    
    // bug:代码跟我的一模一样,但是标题就是显示不出来
    // 内容往下移动,莫名其妙
}


#pragma mark - 处理标题点击
- (void)titleClick:(UIButton *)button
{
    NSInteger i = button.tag;
    
    // 1.标题颜色 变成 红色
    [self selButton:button];
    
 //    CGRectMake(btnX+25, btnH-2-6, 50, 2)
    
   
    // 2.把对应子控制器的view添加上去
    [self setupOneViewController:i];
    
    // 3.内容滚动视图滚动到对应的位置
    CGFloat x = i * [UIScreen mainScreen].bounds.size.width;
    self.contentScrollView.contentOffset = CGPointMake(x, 0);
}

#pragma mark - 选中标题
- (void)selButton:(UIButton *)button
{
    [self.selectButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [button setTitleColor:RGB(26, 113, 255) forState:UIControlStateNormal];
    self.selectButton = button;
    
    NSInteger i = button.tag;
    NSInteger count = self.biaotiArray.count;
    CGFloat  jiaju =  (kScreenW - count *100) / (count+ 1);
    CGFloat btnW = 100;
    CGFloat btnX = i * btnW + jiaju*(i+1);
    self.xiantiaoimageview.frame = CGRectMake(btnX+30, 40-2, 40, 2);
    
    
}

#pragma mark - 添加一个子控制器的View
- (void)setupOneViewController:(NSInteger)i
{
    
    UIViewController *vc = self.childViewControllers[i];
    if (vc.view.superview) {
        return;
    }
    CGFloat x = i * [UIScreen mainScreen].bounds.size.width;
    vc.view.frame = CGRectMake(x, 0,   kScreenW, self.contentScrollView.bounds.size.height);
    [self.contentScrollView addSubview:vc.view];
}





// 只要一滚动就需要字体渐变
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    // 字体缩放 1.缩放比例 2.缩放哪两个按钮
    NSInteger leftI = scrollView.contentOffset.x / kScreenW;

    UIButton *leftBtn = self.titleButtons[leftI];

    // 1.标题颜色 变成 红色
    [self selButton:leftBtn];

    // 添加控制器
    [self setupOneViewController:leftI];


}


@end
